package com.freshcity.page.home
{
	import com.freshcity.component.BaseGameObject;
	import com.freshcity.core.Dispatcher;
	import com.freshcity.core.MainScene;
	import com.freshcity.core.OpCodes;
	import com.freshcity.lib.event.DataEvent;
	import com.freshcity.lib.event.EventName;
	import com.freshcity.manager.MsgManager;
	import com.freshcity.page.talk.ReplyTalk;
	import com.freshcity.page.talk.Talk;
	import com.freshcity.talk.CallIcon;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.pblabs.debug.Logger;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;


	public class CallListPanel extends BaseGameObject
	{
		public var main:MainScene;

		public var box:Sprite;
		private var xpos:Number=750;

		private var calls:Array=[];

		public function CallListPanel(_name:String=null)
		{
			super(_name);
		}

		override public function destroy():void
		{
			super.destroy();
		}

		override public function initialize():void
		{
			super.initialize();

			box=new Sprite();
			box.mouseEnabled=false;
			box.y=55;
			parent.addChild(box);

			main=this.owningGroup as MainScene;
			group.commu.register(OpCodes.REQ_TALK, addCall);
			group.commu.register(OpCodes.START_TALK, onStartTalk);
		}
		
		private function onStartTalk(data:*):void
		{
			main.initTalk();
			if (data is String)
			{
				main.msg.showRollTipsMsg(data);
				return;
			}
			Dispatcher.dispatchEvent(new DataEvent(EventName.StartTalk, {}));
			//ExternalInterface.call("startTalk");
			//group.soundManager.playSound(SoundTypeConst.ComeIn,true);
			main.layer.leave();
			
			var showTalk:Function=function():void
			{
				if(main.currentScene){
					main.currentScene.destroy();
				}
				var talk:Talk=main.talk;
				talk.data=data;
				talk.show();
			};
			
			if(main.currentScene){
				TweenMax.to(main.currentScene, 0.3, {x: "-200", autoAlpha: 0, onComplete: showTalk});
			}else{
				showTalk();
			}
		}
		
		public function addCall(data:*):void
		{
			Logger.debug(this, "addCall", data.id + " is applying talk...");
			group.msg.showFixRollMsg("用户" + data.user.id + "正请求和你聊天...");
			group.msg.showPopUpMsg("用户" + data.user.id + "正请求和你聊天，立刻点击上方呼叫按钮回应TA吧！", MsgManager.HELLO, 5);
			var i:int=calls.length;
			var newMsg:CallIcon=new CallIcon();
			newMsg.buttonMode=true;
			newMsg.x=xpos - calls.length * 40;
			newMsg.data=data;
			newMsg.index=i;
			box.addChild(newMsg);
			calls.push(newMsg);
			newMsg.addEventListener(MouseEvent.CLICK, onShowCall);
			//TweenLite.delayedCall(8,removeCall,[i]);
		}

		protected function onShowCall(event:MouseEvent):void
		{
			var loc1:CallIcon=event.currentTarget as CallIcon;
			removeCall(loc1.index);
			new ReplyTalk(main, loc1.data);
		}

		private function removeCall(index:int):void
		{
			if (calls[index])
			{
				calls[index].parent.removeChild(calls[index]);
				for (var i:int=index; i < calls.length; i++)
				{
					TweenLite.to(calls[i], 0.3, {x: "+40"});
				}
				calls.splice(index, 1);
			}
		}
	}
}